package org.dromara.business.jobhandler;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.business.domain.GoodsOrder;
import org.dromara.business.service.BusinessConfigService;
import org.dromara.business.service.GoodsOrderService;
import org.dromara.business.service.UserService;
import org.dromara.common.core.enums.UserType;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.mybatis.helper.DataPermissionHelper;
import org.dromara.system.domain.SysUser;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;


/**
 * 定时任务
 *
 * @author zlj
 * @since 2023/8/10
 */
@Slf4j
@Component
@EnableScheduling
public class BusinessJobHandler {

    @Resource
    private UserService userService;
    @Resource
    private BusinessConfigService configService;
    @Resource
    private GoodsOrderService goodsOrderService;

    /**
     * 重置每日转盘次数和日收入
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void day() {
        //重置每日转盘次数和日收入
        String numberDraws = configService.queryValueByKey("number_draws");
        DataPermissionHelper.ignore(() ->
            userService.lambdaUpdate()
                .set(SysUser::getNumberDraws, numberDraws)
                .set(SysUser::getDayAmount, BigDecimal.ZERO)
                .eq(SysUser::getUserType, UserType.XCX_USER.getUserType())
                .update()
        );
    }

    /**
     * 重置每周收入
     */
    @Scheduled(cron = "10 0 0 * * 1")
    public void week() {
        //重置每周收入
        DataPermissionHelper.ignore(() ->
            userService.lambdaUpdate()
                .set(SysUser::getWeekAmount, BigDecimal.ZERO)
                .update()
        );
    }

    /**
     * 重置每月收入
     */
    @Scheduled(cron = "20 0 0 1 * ?")
    public void mouth() {
        //重置每周收入
        DataPermissionHelper.ignore(() ->
            userService.lambdaUpdate()
                .set(SysUser::getMonthAmount, BigDecimal.ZERO)
                .update()
        );
    }

    /**
     * 完成订单
     */
    @Scheduled(cron = "30 0 0/1 * * ?")
    public void endOrder() {
        List<GoodsOrder> list = goodsOrderService.lambdaQuery()
            .eq(GoodsOrder::getStatus, "4")
            .le(GoodsOrder::getUpdateTime, DateUtil.offsetHour(DateUtils.getNowDate(), -24))
            .list();
        if (CollectionUtil.isEmpty(list)) {
            return;
        }
        for (GoodsOrder order : list) {
            DataPermissionHelper.ignore(() -> goodsOrderService.end(order.getGoodsOrderId()));
        }
    }

    /**
     * 删除订单
     */
    @Scheduled(cron = "40 0 0 * * ?")
    public void removeOrder() {
        goodsOrderService.lambdaUpdate()
            .eq(GoodsOrder::getStatus, "1")
            .le(GoodsOrder::getCreateTime, DateUtil.offsetHour(DateUtils.getNowDate(), -20))
            .remove();
//        if (CollectionUtil.isEmpty(list)) {
//            return;
//        }
//        for (GoodsOrder order : list) {
//            DataPermissionHelper.ignore(() -> goodsOrderService.removeOrder(order.getGoodsOrderId()));
//        }
    }
}
